package l2ft.gameserver.model.entity.events.impl.fightclub;

import l2ft.commons.collections.MultiValueSet;
import l2ft.gameserver.listener.actor.OnAttackHitListener;
import l2ft.gameserver.model.Creature;
import l2ft.gameserver.model.GameObject;
import l2ft.gameserver.model.Player;
import l2ft.gameserver.model.Skill;
import l2ft.gameserver.model.entity.events.fightclubmanager.FightClubPlayer;
import l2ft.gameserver.model.entity.events.impl.AbstractFightClub;
import l2ft.gameserver.model.items.Inventory;
import l2ft.gameserver.model.items.ItemInstance;

public class FFAHuntingGroundsEvent extends AbstractFightClub
{
	public class HitListener implements OnAttackHitListener
	{
		@Override
		public void onAttackHit(Creature actor, Creature attacker)
		{
			if(actor.isPlayer())
			{
				actor.doDie(attacker);
			}
		}
	}
	
	private static final int BOW_ID = 20261;
	private static final int ARROWS_ID = 17;
	private static final int SHOT_ID = 1835;
	private static final long BULLETS_COUNT = 1000;
	private final int _playerSpd;
	private final int _playerPAtk;
	private HitListener _listener;
	
	public FFAHuntingGroundsEvent(MultiValueSet<String> set)
	{
		super(set);
		_playerSpd = set.getInteger("playerSpd");
		_playerPAtk = set.getInteger("playerPAtk");
		_listener = new HitListener();
	}
	
	@Override
	public void onAddEvent(GameObject o)
	{
		if(o.isPlayer())
		{
			o.getPlayer().addListener(_listener);
		}
		super.onAddEvent(o);
	}
	
	@Override
	public void onRemoveEvent(GameObject o)
	{
		if(o.isPlayer())
		{
			o.getPlayer().removeListener(_listener);
		}
		super.onRemoveEvent(o);
	}
	
	@Override
	public void onKilled(Creature actor, Creature victim, Skill skill)
	{
		if(actor != null && actor.isPlayable())
		{
			FightClubPlayer realActor = getFightClubPlayer(actor.getPlayer());
			if(victim.isPlayer())
			{
				realActor.increaseKills(true);
				realActor.increaseScore(1);
				updatePlayerScore(realActor);
				sendMessageToPlayer(realActor, MESSAGE_TYPES.HERO, "You have killed "+victim.getName());
			}
			else if(victim.isPet())
			{
				
			}
			actor.getPlayer().sendUserInfo();
		}
		
		if(victim.isPlayer())
		{
			FightClubPlayer realVictim = getFightClubPlayer(victim);
			realVictim.increaseDeaths();
			if(actor != null)
				sendMessageToPlayer(realVictim, MESSAGE_TYPES.HERO, "You have been killed by "+actor.getName());
			victim.getPlayer().sendUserInfo();
		}
		
		super.onKilled(actor, victim, skill);
	}
	
	@Override
	public void startEvent()
	{
		super.startEvent();
		
		for(FightClubPlayer iFPlayer : getPlayers(FIGHTING_PLAYERS))
		{
			Player player = iFPlayer.getPlayer();
			ItemInstance bowInstance = player.getInventory().addItem(BOW_ID, 1);
			player.getInventory().addItem(ARROWS_ID, BULLETS_COUNT);
			player.getInventory().addItem(SHOT_ID, BULLETS_COUNT);
			player.getInventory().equipItem(bowInstance);
		}
	}
	
	@Override
	public void stopEvent()
	{
		for(FightClubPlayer iFPlayer : getPlayers(FIGHTING_PLAYERS))
		{
			Player player = iFPlayer.getPlayer();
			removeEventItems(player);
		}
		super.stopEvent();
	}
	
	@Override
	public boolean leaveEvent(Player player, boolean teleportTown)
	{
		removeEventItems(player);
		return super.leaveEvent(player, teleportTown);
	}
	
	private void removeEventItems(Player player)
	{
		player.getInventory().removeItemByItemId(ARROWS_ID, player.getInventory().getCountOf(ARROWS_ID));
		player.getInventory().removeItemByItemId(SHOT_ID, player.getInventory().getCountOf(SHOT_ID));
		player.unEquipWeapon();
		player.getInventory().removeItemByItemId(BOW_ID, 1);
	}
	
	@Override
	public int getMySpeed(Player player)
	{
		return _playerSpd;
	}
	
	@Override
	public int getPAtkSpd(Player player)
	{
		return _playerPAtk;
	}
	
	@Override
	public String getVisibleTitle(Player player, String currentTitle, boolean toMe)
	{
		FightClubPlayer realPlayer = getFightClubPlayer(player);
		
		if(realPlayer == null)
			return currentTitle;
		
		return "Kills: " + realPlayer.getKills(true) + " Deaths: "+realPlayer.getDeaths();
	}
}
